package com.sskj.mine.ui.fragment;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.LoginFragmentPresenter;
import com.sskj.mine.util.LogoutUtil;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 登录界面Fragment
 */
public class LoginFragment extends BaseFragment<LoginFragmentPresenter> {


    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivTogglePwd)
    ImageView ivTogglePwd;
    @BindView(R2.id.btLogin)
    Button btLogin;
    @BindView(R2.id.tvPwdForget)
    TextView tvPwdForget;
    @Inject
    UserViewModel userViewModel;
    private boolean isSee = false;// 密码是否显示

    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment_login;
    }

    @Override
    protected LoginFragmentPresenter getPresenter() {
        return new LoginFragmentPresenter();
    }

    @Override
    public void initView() {
        super.initView();
        LogoutUtil.isInLogin = true;
        DaggerUserDataComponent
                .create()
                .inject(this);
        ClickUtil.click(btLogin, () -> {
            if (CheckUtil.isMobile(etAccount.getText().toString()) || CheckUtil.isEmail(etAccount.getText().toString())) {
                if (etPwd.getText().length() >= 6 && etPwd.getText().length() <= 20) {
                    mPresenter.login(etAccount.getText().toString(), etPwd.getText().toString());
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineLoginActivity0));
                }
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineLoginActivity1));
            }
        });
        ClickUtil.click(100, ivTogglePwd, () -> {
            if (isSee) {
                isSee = false;
                int selectionEnd = etPwd.getSelectionEnd();
                etPwd.setTransformationMethod(new PasswordTransformationMethod());
                etPwd.setSelection(selectionEnd);
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSee = true;
                int selectionEnd = etPwd.getSelectionEnd();

                etPwd.setTransformationMethod(new HideReturnsTransformationMethod());
                etPwd.setSelection(selectionEnd);
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_see);
            }
        });
        ClickUtil.click(tvPwdForget, () -> ARouter.getInstance().build(RConfig.MINE_PWD_FORGET).navigation());
    }

    /**
     * 登录成功
     */
    public void loginSuccess() {
        LogoutUtil.isInLogin = false;
        RxBus.getDefault().send(RxBusCode.LOGIN);
        userViewModel.update();
        getActivity().finish();
    }

    @Override
    public void onDestroy() {
        LogoutUtil.isInLogin = false;
        super.onDestroy();
    }

    public static LoginFragment newInstance() {
        Bundle args = new Bundle();
        LoginFragment fragment = new LoginFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
